package cn.legend.travel.admin.destination.dao.persist.repository.impl;

import cn.legend.travel.admin.destination.dao.persist.mapper.DelicacyMapper;
import cn.legend.travel.admin.destination.dao.persist.repository.IDelicacyRepository;
import cn.legend.travel.admin.destination.pojo.entity.Delicacy;
import cn.legend.travel.admin.destination.pojo.param.DelicacyAddNewParam;
import cn.legend.travel.admin.destination.pojo.param.DelicacyUpdateParam;
import cn.legend.travel.admin.destination.pojo.vo.DelicacyAdminVO;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.vo.PageData;
import cn.legend.travel.common.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

@Repository
@RequiredArgsConstructor
public class DelicacyRepositoryImpl implements IDelicacyRepository {
    private final DelicacyMapper mapper;

    @Override
    public PageData<DelicacyAdminVO> list(int pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return PageInfoToPageDataConverter.convert(new PageInfo<>(mapper.list()));
    }

    @Override
    public int update(DelicacyUpdateParam param) {
        Delicacy delicacy = new Delicacy();
        BeanUtils.copyProperties(param, delicacy);
        return mapper.updateById(delicacy);
    }

    @Override
    public int delete(Long id) {
        return mapper.deleteById(id);
    }

    @Override
    public Long addNew(DelicacyAddNewParam param) {
        Delicacy delicacy = new Delicacy();
        BeanUtils.copyProperties(param, delicacy);
        int row = mapper.create(delicacy);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERROR_INSERT, "服务器忙,请稍后再试.");
        }
        return delicacy.getId();
    }
}
